﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;
using Avatar.Common;

public partial class FriendLinkApply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Apply_Click(object sender, EventArgs e)
    {
        try
        {
            #region 入口检测

            if (!string.IsNullOrEmpty(txt_SiteName.Text) && txt_SiteName.Text.Length > 30) throw new Exception("网站名称不能超过30个字符");
            if (txt_URL.Text.Length > 150) throw new Exception("网站网址不能大于150个字符");

            #endregion
            string _code = HttpContext.Current.Session["ImgCode"] == null ? string.Empty : HttpContext.Current.Session["ImgCode"].ToString();
            if (_code != txt_VCode.Text) throw new Exception("验证码错误！");

            using (var _adc = DBHelper.GetDataContext())
            {
                var _l = _adc.FriendLink.FirstOrDefault(l => l.URL == txt_URL.Text);
                if (_l == null)
                {
                    FriendLink _link = new FriendLink();
                    _link.SiteName = txt_SiteName.Text;
                    _link.URL = txt_URL.Text;
                    _link.Open = false;

                    _adc.FriendLink.InsertOnSubmit(_link);
                    _adc.SubmitChanges();
                }
            }

            txt_SiteName.Text = "";
            txt_URL.Text = "";
            txt_VCode.Text = "";
            Helper.ShowMassage("恭喜：申请成功！请耐心等待本站与您取得联系。");

        }
        catch (Exception _e)
        {
            lbl_Error.Text = "错误:"+_e.Message;
        }
    }
}
